//eat.js
var util = require('../../utils/util.js')
var app = getApp();
var openId;
var userId;
var already=0;
Page({
  data: {
    slider: [],
    subjectImage: [
        '../img/111..png' ,
        '../img/222.png' 
    ],
    showshow:false,
    priceBuy:true,
    priceNotBuy: false,
    swiperCurrent: 0,
    duration: 500,
    indicatorDots: false,
    interval: '5000',
    courseList:[],
    hiddenLoading: false 
  },
  onLoad: function () {
      let that=this;
      // 获取用户信息
      wx.getSetting({
          success: res => {
              console.log(res.authSetting);
              if (!res.authSetting['scope.userInfo']) {
                      wx.getUserInfo({
                          success: res => {
                              console.log(res);
                              // 可以将 res 发送给后台解码出 unionId
                              app.globalData.userInfo = res.userInfo;
                              console.log(res.userInfo);
                              console.log(app.globalData.userInfo);
                              wx.setStorageSync("userInfo", res.userInfo);
                              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                              wx.request({
                                  url: app.globalData.url + "/getAppletUnionId",
                                  data: {
                                      encryptedData: res.encryptedData,
                                      iv: res.iv,
                                      code: wx.getStorageSync("code")
                                  },
                                  success: function (res) {
                                      console.log(res);
                                      wx.setStorageSync("userId", res.data.userId);
                                      wx.setStorageSync("openId", res.data.unionId);
                                      wx.setStorageSync("openIds", res.data.token.openId);
                                      app.globalData.openId = res.data.unionId;
                                      wx.showLoading({
                                          title: '加载中',
                                          mask: true
                                      }); 
                                      that.courseList();
                                      that.bannerList();
                                      
                                  }
                              })
                          },
                          fail: function () {
                              console.log("拒绝授权");
                              wx.showModal({
                                  title: '再次授权',
                                  content: '您点击了拒绝授权，您在使用过程中可能会出现问题，为了保障您正常使用，请点击"前往授权"进行授权',
                                  showCancel: false,
                                  confirmText: "前往授权",
                                  success: function (res) {
                                      if (res.confirm) {
                                          console.log('用户点击确定')
                                          wx.openSetting({
                                              success: function success(res) {
                                                  console.log('openSetting success', res.authSetting);
                                                  console.log(res);
                                                  wx.getUserInfo({
                                                      success: res => {
                                                          console.log(res);
                                                          app.globalData.userInfo = res.userInfo
                                                          // 可以将 res 发送给后台解码出 unionId
                                                          wx.setStorageSync("userInfo", res.userInfo);
                                                          wx.request({
                                                              url: app.globalData.url + "/getAppletUnionId",
                                                              data: {
                                                                  encryptedData: res.encryptedData,
                                                                  iv: res.iv,
                                                                  code: wx.getStorageSync("code")
                                                              },
                                                              success: function (res) {
                                                                  console.log(res);
                                                                  wx.setStorageSync("userId", res.data.userId);
                                                                  wx.setStorageSync("openId", res.data.unionId);
                                                                  wx.setStorageSync("openIds", res.data.token.openId);
                                                                  app.globalData.openId = res.data.unionId;
                                                                  wx.showLoading({
                                                                      title: '加载中',
                                                                      mask: true
                                                                  });
                                                                  that.courseList();
                                                                  that.bannerList();


                                                              }
                                                          })
                                                      },
                                                      fail: function () {
                                                          that.checkSettingStatu();
                                                      }
                                                  })
                                              }
                                          });
                                      }
                                  }
                              })
                          }
                      })
              }else{
                  console.log(3)
                  already=1;
                  wx.setStorageSync("already", 1);
              }
          }
      })
  },
  // 授权
  checkSettingStatu: function (cb) {
      let that = this;
      // 判断是否是第一次授权，非第一次授权且授权失败则进行提醒
      wx.getSetting({
          success: function success(res) {
            let authSetting = res.authSetting;
            console.log('不是第一次授权', authSetting);
            // 没有授权的提醒
            if (authSetting['scope.userInfo'] === false) {
                wx.showModal({
                    title: '再次授权',
                    content: '您点击了拒绝授权，您在使用过程中可能会出现问题，为了保障您正常使用，请点击"前往授权"进行授权',
                    showCancel: false,
                    confirmText: "前往授权",
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                            wx.openSetting({
                                success: function success(res) {
                                    console.log('openSetting success', res.authSetting);
                                            console.log(res);
                                            wx.getUserInfo({
                                                success: res => {
                                                    console.log(res);
                                                    app.globalData.userInfo = res.userInfo
                                                    // 可以将 res 发送给后台解码出 unionId
                                                    wx.setStorageSync("userInfo", res.userInfo);
                                                    wx.request({
                                                        url: app.globalData.url + "/getAppletUnionId",
                                                        data: {
                                                            encryptedData: res.encryptedData,
                                                            iv: res.iv,
                                                            code: wx.getStorageSync("code")
                                                        },
                                                        success: function (res) {
                                                            console.log(res);
                                                            wx.setStorageSync("userId", res.data.userId);
                                                            wx.setStorageSync("openId", res.data.unionId);
                                                            wx.setStorageSync("openIds", res.data.token.openId);
                                                            app.globalData.openId = res.data.unionId;
                                                            wx.showLoading({
                                                                title: '加载中',
                                                                mask: true
                                                            }); 
                                                            that.courseList();
                                                            that.bannerList();
                                                            

                                                        }
                                                    })
                                                },
                                                fail:function(){
                                                    that.checkSettingStatu();
                                                }
                                            })  
                                }
                            });
                        }
                    }
                })
            }
              
          }
      });
  },
  bannerList: function () {
      let that = this;
      wx.request({
          url: app.globalData.url + "/AppletIndexBanner",
          data: {
              
          },
          success: function (res) {
              console.log(res);
              that.setData({
                  slider: res.data.bannerList
              })
            //   wx.hideLoading();
          }
      })
  },
  courseList:function(){
    let that=this;
    openId = wx.getStorageSync("openId");
    userId= wx.getStorageSync("userId");
    wx.request({
        url: app.globalData.url + "/getAppletCourseList",
        data: {
            openId: openId,
            userId: userId
        },
        success: function (res) {
            console.log(res.data.courseList);    //   直接进入播放页面
    
            let cl = res.data.courseList;
            wx.hideLoading();
            wx.hideNavigationBarLoading();
            that.setData({
                courseList: cl,
                showshow:true
            })
            if (already==0){
                already = 1;
                wx.setStorageSync("already", 1);
            
                wx.request({// 获取第一个课程
                    url: app.globalData.url + "/getAppletCourse",
                    data: {
                        openId: openId,
                        productId: cl[0].productId

                    },
                    success: function (res) {
                        console.log(res);
                        wx.setStorageSync('letCourse', res.data)//缓存某老师的课程介绍
                    }
                })

                wx.request({//获取第一个课程详情
                    url: app.globalData.url + "/getAppletLessonList",
                    data: {
                        openId: openId,
                        userId: userId,
                        productId: cl[0].productId

                    },
                    success: function (res) {
                        wx.setStorageSync('letLessonList', res.data.Lesson)//缓存某老师的课
                    }
                })
            }
            

        }
    })   
  },
//   课程跳转
  goPage: function (e) {
      console.log(e)
      let productid = e.currentTarget.dataset.productid;
      let url = e.currentTarget.dataset.url;     
      wx.setStorageSync('productId', productid);
    
      wx.navigateTo({
          url:url+'?productId='+productid
      });
  },
  // 广告跳转
  gobanner:function(e){
      console.log(e);
      let adtype = e.currentTarget.dataset.adtype;
      let url = e.currentTarget.dataset.url;
      let urlin = e.currentTarget.dataset.urlin;
      let productid = e.currentTarget.dataset.innerpicurl;
      wx.setStorageSync('productId', productid);
      //{1,图片"},{4, "广告" },{5,"产品" }
      if (adtype==4){
          wx.navigateTo({
              url: '/pages/ad/ad'
          });
      } else if (adtype == 5){
          console.log(5);
          wx.navigateTo({
              url: urlin + '?productId=' + productid 
          });
      }else{
          console.log(1);
      }
     
  },
  goDown: function (e) {
      console.log(e)
      let url = e.currentTarget.dataset.url;
      wx.navigateTo({
          url: url
      });
  },
  
  // 分享功能
  onShareAppMessage: function (options) {
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
        title: "i幸会小程序",        // 默认是小程序的名称(可以写slogan等)
        path:'/pages/main/main',        // 默认是当前页面，必须是以‘/’开头的完整路径
        imageUrl:'../img/touxiang.jpg',     //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
          console.log(res);
          wx.showShareMenu({
            withShareTicket: true,
            success:function(){
            }
          })
        }
      },
      fail: function (res) {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
        }
      },
      complete: function () {
        // 转发结束之后的回调（转发成不成功都会执行）
      }
    };
    // 来自页面内的按钮的转发
    if (options.from == 'button') {
      var eData = options.target.dataset;
      console.log(eData.name);     // shareBtn
      // 此处可以修改 shareObj 中的内容
      shareObj.path = '/pages/btnname/btnname?btn_name=' + eData.name;
    }
    　　// 返回shareObj
    　　return shareObj;
  },
  onShow:function(){
      already = wx.getStorageSync("already");
      if (already==1){
          wx.showNavigationBarLoading();
          this.courseList();
          this.bannerList();
      }
     
  }
})